package com.fengmoxi.ctv.activity.config.lamp;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.leanback.app.BackgroundManager;
import androidx.leanback.app.BrowseSupportFragment;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.HeaderItem;
import androidx.leanback.widget.ListRow;
import androidx.leanback.widget.ListRowPresenter;
import androidx.leanback.widget.OnItemViewClickedListener;
import androidx.leanback.widget.OnItemViewSelectedListener;
import androidx.leanback.widget.Presenter;
import androidx.leanback.widget.Row;
import androidx.leanback.widget.RowPresenter;

import com.fengmoxi.ctv.Const;
import com.fengmoxi.ctv.R;
import com.fengmoxi.ctv.activity.config.lamp.other.ConfigLampAboutActivity;
import com.fengmoxi.ctv.activity.config.lamp.other.ConfigLampInitParamActivity;
import com.fengmoxi.ctv.activity.config.lamp.pq.ConfigLampBrightnessActivity;
import com.fengmoxi.ctv.activity.config.lamp.pq.ConfigLampContrastActivity;
import com.fengmoxi.ctv.activity.config.lamp.pq.ConfigLampHueActivity;
import com.fengmoxi.ctv.activity.config.lamp.pq.ConfigLampSaturationActivity;
import com.fengmoxi.ctv.activity.config.lamp.pq.ConfigLampSharpnessActivity;
import com.fengmoxi.ctv.activity.config.lamp.vc.ConfigLampBiphaseActivity;
import com.fengmoxi.ctv.activity.config.lamp.vc.ConfigLampOpticalAlignmentActivity;
import com.fengmoxi.ctv.activity.config.lamp.vc.ConfigLampPositionActivity;
import com.fengmoxi.ctv.manage.HomeButtonReceiver;
import com.fengmoxi.ctv.manage.LampManage;

public class ConfigLampActivity extends FragmentActivity {
    private static final String TAG = ConfigLampActivity.class.getSimpleName();
    private BrowseSupportFragment mBrowseFragment;
    private ArrayObjectAdapter rowsAdapter;
    private LampManage mLampMange;
    private HomeButtonReceiver mHomeButtonReceiver;
    public static ConfigLampActivity configLampActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_lamp);

        configLampActivity = this;

        mBrowseFragment = (BrowseSupportFragment) getSupportFragmentManager().findFragmentById(R.id.conf_lamp_fragment);

        BackgroundManager mBackgroundManager = BackgroundManager.getInstance(this);
        mBackgroundManager.attach(getWindow());
        DisplayMetrics mMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(mMetrics);

        mLampMange = new LampManage();

        assert mBrowseFragment != null;
        mBrowseFragment.setHeadersState(BrowseSupportFragment.HEADERS_DISABLED);
        mBrowseFragment.setTitle(getString(R.string.app_item_config_lamp));

        setupRowsAdapter();

        mHomeButtonReceiver = new HomeButtonReceiver(this, new HomeButtonReceiver.OnHomeBtnPressListener() {
            @Override
            public void onHomeBtnPress() {
                finish();
            }

            @Override
            public void onHomeBtnLongPress() {

            }
        });
        mHomeButtonReceiver.start();
    }

    @Override
    protected void onDestroy() {
        mHomeButtonReceiver.stop();
        super.onDestroy();
    }

    private void setupRowsAdapter() {
        rowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
        setVcRows();// 画面质量行
        setPqRows(); // 画面控制行
        setInfoRows();// 信息行
        mBrowseFragment.setAdapter(rowsAdapter);
        mBrowseFragment.setOnItemViewClickedListener(new ItemViewClickedListener());
        mBrowseFragment.setOnItemViewSelectedListener(new ItemViewSelectedListener());
    }

    private ArrayObjectAdapter vcGridRowAdapter;

    // Video Control Command
    private void setVcRows() {
        HeaderItem gridHeader = new HeaderItem(1, getResources().getString(R.string.conf_lamp_vc));
        GridItemPresenter mGridPresenter = new GridItemPresenter();
        vcGridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        vcGridRowAdapter.add(R.string.conf_lamp_vc_position);
        vcGridRowAdapter.add(R.string.conf_lamp_vc_optical_alignment);
        vcGridRowAdapter.add(R.string.conf_lamp_vc_biphase);
        vcGridRowAdapter.add(R.string.conf_lamp_vc_flip);
        rowsAdapter.add(new ListRow(gridHeader, vcGridRowAdapter));
        mBrowseFragment.setAdapter(rowsAdapter);
        refreshFlip();
    }

    private void refreshFlip() {
        LampManage.Position pos = mLampMange.getPosition();
        if (pos.getFlip() == 0) {
            vcGridRowAdapter.replace(3, R.string.conf_lamp_vc_flip_off);
        } else if (pos.getFlip() == 3) {
            vcGridRowAdapter.replace(3, R.string.conf_lamp_vc_flip_on);
        }
    }

    // Video PQ Control Command
    private void setPqRows() {
        HeaderItem gridHeader = new HeaderItem(2, getResources().getString(R.string.conf_lamp_pq));
        GridItemPresenter mGridPresenter = new GridItemPresenter();
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        gridRowAdapter.add(R.string.conf_lamp_pq_brightness);
        gridRowAdapter.add(R.string.conf_lamp_pq_contrast);
        gridRowAdapter.add(R.string.conf_lamp_pq_hue);
        gridRowAdapter.add(R.string.conf_lamp_pq_saturation);
        gridRowAdapter.add(R.string.conf_lamp_pq_sharpness);
        rowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));
        mBrowseFragment.setAdapter(rowsAdapter);
    }

    // Info
    private void setInfoRows() {
        HeaderItem gridHeader = new HeaderItem(3, getResources().getString(R.string.conf_lamp_info));
        GridItemPresenter mGridPresenter = new GridItemPresenter();
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        gridRowAdapter.add(R.string.conf_lamp_info_about);
        gridRowAdapter.add(R.string.conf_lamp_info_init);
        rowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));
        mBrowseFragment.setAdapter(rowsAdapter);
    }

    private static class GridItemPresenter extends Presenter {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent) {
            TextView view = new TextView(parent.getContext());
            view.setLayoutParams(new ViewGroup.LayoutParams(Const.CARD_CONFIG_LAMP_WIDTH, Const.CARD_CONFIG_LAMP_HEIGHT));
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.setBackgroundColor(
                    ContextCompat.getColor(configLampActivity, R.color.default_background));
            view.setTextColor(Color.WHITE);
            view.setGravity(Gravity.CENTER);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, Object item) {
            ((TextView) viewHolder.view).setText((int) item);
        }

        @Override
        public void onUnbindViewHolder(ViewHolder viewHolder) {
        }
    }

    private final class ItemViewClickedListener implements OnItemViewClickedListener {
        @Override
        public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
            if (item instanceof Integer) {
                int method = (Integer) item;
                Log.d(TAG, "Click method: " + configLampActivity.getString(method));
                if (method == R.string.conf_lamp_info_about) {
                    startActivity(new Intent(configLampActivity, ConfigLampAboutActivity.class));
                } else if (method == R.string.conf_lamp_info_init) {
                    startActivity(new Intent(configLampActivity, ConfigLampInitParamActivity.class));
                } else if (method == R.string.conf_lamp_pq_brightness) {
                    startActivity(new Intent(configLampActivity, ConfigLampBrightnessActivity.class));
                } else if (method == R.string.conf_lamp_pq_contrast) {
                    startActivity(new Intent(configLampActivity, ConfigLampContrastActivity.class));
                } else if (method == R.string.conf_lamp_pq_hue) {
                    startActivity(new Intent(configLampActivity, ConfigLampHueActivity.class));
                } else if (method == R.string.conf_lamp_pq_saturation) {
                    startActivity(new Intent(configLampActivity, ConfigLampSaturationActivity.class));
                } else if (method == R.string.conf_lamp_pq_sharpness) {
                    startActivity(new Intent(configLampActivity, ConfigLampSharpnessActivity.class));
                } else if (method == R.string.conf_lamp_vc_optical_alignment) {
                    startActivity(new Intent(configLampActivity, ConfigLampOpticalAlignmentActivity.class));
                } else if (method == R.string.conf_lamp_vc_biphase) {
                    startActivity(new Intent(configLampActivity, ConfigLampBiphaseActivity.class));
                } else if (method == R.string.conf_lamp_vc_flip_off) {
                    LampManage.Position pos = mLampMange.getPosition();
                    pos.setFlip(3);
                    mLampMange.setPosition(pos);
                    refreshFlip();
                    mLampMange.savePositionUserParam();
                } else if (method == R.string.conf_lamp_vc_flip_on) {
                    LampManage.Position pos = mLampMange.getPosition();
                    pos.setFlip(0);
                    mLampMange.setPosition(pos);
                    refreshFlip();
                    mLampMange.savePositionUserParam();
                } else if (method == R.string.conf_lamp_vc_position) {
                    startActivity(new Intent(configLampActivity, ConfigLampPositionActivity.class));
                }
            }
        }
    }

    private static final class ItemViewSelectedListener implements OnItemViewSelectedListener {
        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {

        }
    }
}
